/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

/**
 *
 * @author User
 */
import entity.Orders;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.DisabledOrdersManager;
import manager.OrdersManager;

public class OrdersDB implements IManagerDB<OrdersManager, DisabledOrdersManager> {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public OrdersDB(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void loadData(OrdersManager t, boolean value) {
        try {
            cstmt = conn.prepareCall("{call sp_GetOrders(?)}");
            cstmt.setBoolean("Status", value);
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int ordersID = rs.getInt("OrdersID");
                int employeeID = rs.getInt("EmployeeID");
                Date issueDate = rs.getDate("IssueDate");
                Date dueDate = rs.getDate("DueDate");
                boolean returned = rs.getBoolean("Returned");
                boolean status = rs.getBoolean("Status");
                Orders orders = new Orders(ordersID, employeeID, issueDate, dueDate, returned, status);
                t.add(orders);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OrdersDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void insert(Object object) {
        Orders orders = (Orders) object;
        try {
            cstmt = conn.prepareCall("{call sp_InsertOrders(?,?,?,?,?,?)}");
            cstmt.registerOutParameter(1, Types.INTEGER);
            cstmt.setInt(2, orders.getEmployeeID());
            cstmt.setDate(3, new java.sql.Date(orders.getIssueDate().getTime()));
            cstmt.setDate(4, new java.sql.Date(orders.getDueDate().getTime()));
            cstmt.setBoolean(5, orders.isReturned());
            cstmt.setBoolean(6, orders.isStatus());
            cstmt.executeUpdate();
            orders.setOrdersID(cstmt.getInt("OrdersID"));
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(OrdersDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
        Orders orders = (Orders) object;
        try {
            cstmt = conn.prepareCall("{call sp_UpdateOrders(?,?,?,?,?,?)}");
            cstmt.setInt(1, orders.getOrdersID());
            cstmt.setInt(2, orders.getEmployeeID());
            cstmt.setDate(3, new java.sql.Date(orders.getIssueDate().getTime()));
            cstmt.setDate(4, new java.sql.Date(orders.getDueDate().getTime()));
            cstmt.setBoolean(5, orders.isReturned());
            cstmt.setBoolean(6, orders.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(OrdersDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}